package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityBookRanksBinding
import com.starsea.book.ui.adapter.MyFragmentStateAdapter
import com.starsea.book.ui.newpage.adapter.BookRanksTitleAdapter
import com.starsea.book.ui.newpage.entity.BookTabTitleEntity
import com.starsea.book.ui.newpage.fragment.BookRanksParentFragment
import com.starsea.book.ui.newpage.fragment.HomeChildFragment
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.utils.viewbindingdelegate.viewBinding
import java.io.Serializable

/**
 * @author Lee
 * @desc :
 */
class BookRanksActivity : VMBaseActivity<ActivityBookRanksBinding, HomeViewModel>() {

    lateinit var mAdapter: BookRanksTitleAdapter

    private lateinit var mFragmentAdapter: MyFragmentStateAdapter

    var mFragentList: MutableList<Fragment> = arrayListOf()

    var value = ""

    override val viewModel: HomeViewModel by viewModels<HomeViewModel>()
    override val binding: ActivityBookRanksBinding by viewBinding(ActivityBookRanksBinding::inflate)
    override fun onActivityCreated(savedInstanceState: Bundle?) {

        BarUtils.addMarginTopEqualStatusBarHeight(binding.llTitle)

        binding.ivBack.setOnClickListener { finish() }

        mAdapter = BookRanksTitleAdapter(this)
        binding.rvTitle.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        binding.rvTitle.adapter = mAdapter

        value = intent.getStringExtra("value") ?: ""

        viewModel.getBookRanksTitleData(value)

        mAdapter.addOnItemChildClickListener(R.id.tv_title) { _, _, position ->
            mAdapter.items.forEach { it.isSelect = false }
            mAdapter.getItem(position = position)!!.isSelect = true
            mAdapter.notifyDataSetChanged()
            binding.vpContent.currentItem = position
        }


        viewModel.bookRanksTitleResutl.observe(this) {
            when (it.status) {
                200 -> {
                    mFragentList = arrayListOf()
                    it.data!!.typeList.forEach { child ->
                        mFragentList.add(
                            BookRanksParentFragment.newInstance(
                                child.value,
                                it.data!!.rankList as Serializable
                            )
                        )
                    }

                    mAdapter.submitList(it.data!!.typeList)
                    mFragmentAdapter = MyFragmentStateAdapter(mFragentList, this)
                    binding.vpContent.adapter = mFragmentAdapter
//                    binding.vpContent.isUserInputEnabled = false
                    binding.vpContent.offscreenPageLimit = mFragentList.size

                    mAdapter.addOnItemChildClickListener(R.id.tv_title) { _, _, position ->
                        mAdapter.items.forEach { it.isSelect = false }
                        mAdapter.getItem(position = position)!!.isSelect = true
                        mAdapter.notifyDataSetChanged()
                        binding.vpContent.currentItem = position
                    }

                    binding.vpContent.registerOnPageChangeCallback(object :
                        ViewPager2.OnPageChangeCallback() {
                        override fun onPageSelected(position: Int) {
                            super.onPageSelected(position)
                            mAdapter.items.forEach { it.isSelect = false }
                            mAdapter.getItem(position = position)!!.isSelect = true
                            mAdapter.notifyDataSetChanged()
                        }
                    })

                }

                else -> {

                }
            }
        }


    }


}